<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/4
 * Time: 14:23
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\BoxAreaModel;
use App\Http\Model\Mysql\ShopModel;
use App\Http\Model\Mysql\UserModel;
use App\Http\Model\Mysql\OwnerConnectModel;

class AreaService extends BaseService
{
    /**
     * 地区列表
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function areaList(int $limit = NULL, int $page = 1)
    {
        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $model  =   BoxAreaModel::query();
        $count  =   $model->count();
        $list   =   $model->offset(($page - 1) * $limit)->limit($limit)->select(['id', 'area_name', 'area_desc'])
                    ->get()->each(function (&$item) {
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $limit),
                'current'=> $page
            ]];
        }
    }

    /**
     * 盒子店铺
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function areaShop(int $limit = NULL, int $page = 1)
    {
        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $model  =   ShopModel::query()->active();
        $count  =   $model->count();
        $list   =   $model->offset(($page - 1) * $limit)->limit($limit)->select(['id', 'unique_id', 'shop_name', 'shop_contact', 'shop_logo', 'app_id', 'created_at'])
                    ->get()->each(function (&$item) {
                        $item->create_time  =   $item->created_at->toDateTimeString();
                        unset($item->created_at);
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $limit),
                'current'=> $page
            ]];
        }
    }

    /**
     * 店铺用户列表
     * @param int $shop_id
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function areaConnect(int $shop_id, int $limit = NULL, int $page = 1)
    {
        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $model  =   UserModel::query()->where(['shop_id' => $shop_id]);
        $count  =   $model->count();
        $list   =   $model->offset(($page - 1) * $limit)->limit($limit)->select(['uid', 'unique_id', 'nickname', 'headimg'])
                    ->get()->each(function (&$item) {
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $limit),
                'current'=> $page
            ]];
        }
    }

    /**
     * 关联店铺用户
     * @param int $owner_id
     * @param int $shop_id
     * @param int $user_id
     * @return array
     */
    public function areaBind(int $owner_id, int $shop_id, int $user_id)
    {
        if (!UserModel::where(['shop_id' => $shop_id, 'uid' => $user_id])->exists()) {
            return [false, '该用户不处于店铺中'];
        }

        if (OwnerConnectModel::where(['owner_id' => $owner_id, 'shop_id' => $shop_id, 'user_id' => $user_id])->exists()) {
            return [false, '已关联过了'];
        }

        $result =   OwnerConnectModel::create(['owner_id' => $owner_id, 'shop_id' => $shop_id, 'user_id' => $user_id]);
        if ($result) {
            return [true, '关联成功'];
        } else {
            return [false, '关联失败'];
        }
    }

    /**
     * 盒子区域添加
     * @param string $area_name
     * @param string $area_desc
     * @return array
     */
    public function areaAdd(string $area_name, string $area_desc)
    {
        if (BoxAreaModel::where(['area_name' => $area_name])->exists()) {
            return [false, '区域已存在'];
        }

        $result =   BoxAreaModel::create(['area_name' => $area_name, 'area_desc' => $area_desc]);
        if ($result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 盒子区域编辑
     * @param int $area_id
     * @param string $area_name
     * @param string $area_desc
     * @return array
     */
    public function areaEdit(int $area_id, string $area_name, string $area_desc)
    {
        $result =   BoxAreaModel::where(['id' => $area_id])->update(['area_name' => $area_name, 'area_desc' => $area_desc]);
        if ($result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 盒子区域删除
     * @param int $area_id
     * @return array
     */
    public function areaDelete(int $area_id)
    {
        $result =   BoxAreaModel::where(['id' => $area_id])->delete();
        if ($result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }
}
